const model = require('../model');
const passport = require('../auth/passport_config');
const md5 = require('md5');

//接口调用信息返回
const success = result => ({ code: 0, data: result, msg: 'success' });
const fail = err => ({ code: 1, data: null, msg: err.message || 'fail' });

const login = async (ctx, next) => {
  const body = ctx.request.body;
  await passport.authenticate('local', async (err, user, info, status) => {
    if (err) throw err;
    if (user) {
      ctx.login(user);
      ctx.body = {
        code: 0,
        data: user,
        msg: 'login success',
      }; 
    } else {
      ctx.status = 401;
      ctx.body = {
        code: -1,
        msg: 'fail login',
      }
    };
  })(ctx, next);
}

const logout = async (ctx) => {
  ctx.body = 'hello world';
}

const getUserInfo = async (ctx) => {
  const id = ctx.request.query.id || 1;
  const result = await model.user.getUserById(id);

  ctx.body = result;
}


module.exports = {
  login,
  logout,
  getUserInfo,
}